<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: person.php 142 2012-01-24 22:24:34Z info@sah-company.com $
 */

/**
 * Kalaisoo Person Model Class.
 *
 * @package Kalaisoo
 * @subpackage Model
 * @author $Author: info@sah-company.com $
 */
class Model_Person extends Model
{
	/**
	 * __construct.
	 */
	public function construct()
	{
		parent::__construct();
	}
	
	/**
	 * returns the tags of a bean.
	 *
	 * @return array
	 */
	public function getTags() {
		return array(
			$this->bean->nickname,
			$this->bean->organization,
			$this->bean->first_name,
			$this->bean->last_name,
			$this->bean->home_page,
			$this->bean->department,
			$this->bean->job_title
		);
	}
	
	/**
	 * loads the array $this->gestalt of this bean for use with Model::gestalt().
	 */
	public function loadGestalten()
	{
		$this->gestalt['default'] = array(
			array(
				'type' => 'hidden',
				'name' => 'name',
				'parameters' => array(
				)
			),
			array(
				'type' => 'text',
				'name' => 'nickname',
				'parameters' => array(
					'required' => 'required'
				)
			),
			array(
				'type' => 'text',
				'name' => 'title',
				'parameters' => array(
				)
			),
			array(
				'type' => 'text',
				'name' => 'first_name',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'text',
				'name' => 'last_name',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'text',
				'name' => 'suffix',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'textarea',
				'name' => 'organization',
				'parameters' => array(
					'cols' => 60,
					'rows' => 2
				)
			),
			array(
				'type' => 'checkbox',
				'name' => 'company',
				'parameters' => array(
					'value' => 1	
				)
			),
			array(
				'type' => 'text',
				'name' => 'job_title',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'text',
				'name' => 'department',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'url',
				'name' => 'home_page',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'date',
				'name' => 'birth_date',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'file',
				'name' => 'image',
				'parameters' => array(
					'accept' => 'image/jpeg'
				)
			),
			array(
				'type' => 'textarea',
				'name' => 'note',
				'parameters' => array(
					'cols' => 60,
					'row' => 4
				)
			),
			array(
				'type' => 'text',
				'name' => 'middle_name',
				'parameters' => array(	
				)
			),
			array(
				'type' => 'text',
				'name' => 'maiden_name',
				'parameters' => array(	
				)
			)
		);
	}
	
	/**
	 * validates the data and returns either true or false.
	 *
	 * @return bool
	 */
	public function validate()
	{
		$valid = true;
		return $valid;
	}

	/**
	 * open.
	 */
	public function open()
	{
	}

	/**
	 * dispense.
	 */
	public function dispense()
	{
		$this->bean->setMeta('buildcommand.unique', array(array('nickname')));
	}

	/**
	 * update.
	 */
	public function update()
	{
		// set the phonetic names
		$this->bean->phonetic_last_name = soundex($this->bean->last_name);
		$this->bean->phonetic_middle_name = soundex($this->bean->middle_name);
		$this->bean->phonetic_first_name = soundex($this->bean->first_name);
		// set the name according to sort rule
		$this->bean->name = implode(' ', array($this->bean->first_name, $this->bean->last_name));
		// company name
		if (trim($this->bean->name) == '' && $this->bean->organization || $this->bean->company) {
			$this->bean->name = $this->bean->organization;
		}
		if (trim($this->bean->name) == '') {
			$this->bean->name = $this->bean->nickname;
		}
		// make a vcard of this
		$this->bean->vcard = 'This will be the vcard of this addressbook entry later';
		// maybe image file was uploaded?
		if ($file = Filesystem::moveUploadedFile('uploaded_file')) {
			$this->bean->image = $file;
		}
	}
	
	/**
	 * after_update.
	 */
	public function after_update()
	{
	}
	
	/**
	 * delete.
	 */
	public function delete()
	{
	}
	
	/**
	 * after_delete.
	 */
	public function after_delete()
	{
	}
}
?>